/*
* @Author: dengjiayao
* @Date:   2017-04-25 19:22:46
 * @Last Modified by: songwh
 * @Last Modified time: 2017-05-05 09:58:53
*/

import Vue from 'vue'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const getQuestionListRes = Vue.resource(API_ROOT + '/search/getquestionlist')
export const getAIQuestionList = ({dispatch}, params) => {
  return getQuestionListRes.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('AI_QUESTION_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const addQuestionRes = Vue.resource(API_ROOT + '/search/addquestion')
export const addAIQuestion = ({dispatch}, params) => {
  return addQuestionRes.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('AI_QUESTION_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const updateQuestionRes = Vue.resource(API_ROOT + '/search/updatequestion')
export const updateAIQuestion = ({dispatch}, params) => {
  return updateQuestionRes.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('AI_QUESTION_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const getGuideListRes = Vue.resource(API_ROOT + '/search/getwelcomelist')
export const getAIGuideList = ({dispatch}, params) => {
  return getGuideListRes.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('AI_QUESTION_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const addGuideRes = Vue.resource(API_ROOT + '/search/addwelcome')
export const addAIGuide = ({dispatch}, params) => {
  return addGuideRes.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('AI_QUESTION_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const updateGuideRes = Vue.resource(API_ROOT + '/search/updatewelcome')
export const updateAIGuide = ({dispatch}, params) => {
  return updateGuideRes.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('AI_QUESTION_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const getLogRes = Vue.resource(API_ROOT + '/search/getsearchloglist')
export const getAIQuestionLog = ({dispatch}, params) => {
  return getLogRes.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('AI_QUESTION_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 回答收索没有结果
 */
const answerSearchNoresultRes = Vue.resource(API_ROOT + '/search/answersearchnoresult')
export const answerSearchNoresult = ({dispatch}, params) => {
  return answerSearchNoresultRes.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('AI_QUESTION_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}